package com.example.demo.quartz.web;

import javax.servlet.http.HttpServletRequest;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.annotation.ApiDesc;
import com.example.demo.entity.base.PageRequestBean;
import com.example.demo.entity.base.PageResponseBean;
import com.example.demo.entity.base.ResponseEntity;
import com.example.demo.entity.quartz.QuartzEntity;
import com.example.demo.quartz.job.ChickenJob;
import com.example.demo.quartz.service.QuartzManagerService;

@RestController
@RequestMapping(value="/quartz/manager")
public class QuartzManagerController {
	
	@Autowired
	private QuartzManagerService quartzManagerService;
	
	@ApiDesc("添加定时任务")
	@RequestMapping(value="/addjob")
	public Object addJob(HttpServletRequest request,QuartzEntity quartz) {
		
		ResponseEntity<String> ret = quartzManagerService.addJob(quartz);
		
		return ret;
	}
	
	@ApiDesc("单次调用任务")
	@RequestMapping(value="/trigger")
	public Object trigger(HttpServletRequest request,QuartzEntity quartz) {
		
		ResponseEntity<String> ret = quartzManagerService.trigger(quartz);
		
		return ret;
	}
	
	@ApiDesc("暂停定时任务")
	@RequestMapping(value="/pause")
	public Object pause(HttpServletRequest request,QuartzEntity quartz) {
		
		ResponseEntity<String> ret = quartzManagerService.pause(quartz);
		
		return ret;
	}
	
	@ApiDesc("恢复定时任务的运行")
	@RequestMapping(value="/resume")
	public Object resume(HttpServletRequest request,QuartzEntity quartz) {
		
		ResponseEntity<String> ret = quartzManagerService.resume(quartz);
		
		return ret;
	}
	
	@ApiDesc("删除定时任务")
	@RequestMapping(value="/remove")
	public Object remove(HttpServletRequest request,QuartzEntity quartz) {
		
		ResponseEntity<String> ret = quartzManagerService.remove(quartz);
		
		return ret;
	}
	
	@ApiDesc("分页查询定时任务列表")
	@RequestMapping(value="/queryquartzjoblist")
	public Object queryQuartzJobList(HttpServletRequest request,@RequestBody PageRequestBean<QuartzEntity> pagerequest) {
		
		ResponseEntity<PageResponseBean<QuartzEntity>> ret = quartzManagerService.queryQuartzJobList(pagerequest);
		
		return ret;
	}
	
	@RequestMapping(value="/manualaddtrigger")
	public void manualAddTrigger() throws SchedulerException {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		
		JobDetail jobDetail = JobBuilder.newJob(ChickenJob.class).withIdentity("xzwManual", "xzwQuartz").withDescription("测试手动新建定时任务").build();
		
		jobDetail.getJobDataMap().put("jobMethodName", "seeSeeQuartz");
		
		CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?"); 
		
		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger_xzwManual", "trigger_xzwQuartz")
						.startNow().withSchedule(cronScheduleBuilder).build();
		
		scheduler.scheduleJob(jobDetail,trigger);
	}
	
	@RequestMapping(value="/manualstoptrigger")
	public void manualStopTrigger() throws SchedulerException {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		JobKey jobKey = new JobKey("xzwManual", "xzwQuartz");
		scheduler.pauseJob(jobKey);
	}
	
	@RequestMapping(value="/manualstarttrigger")
	public void manualStartTrigger() throws SchedulerException {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		JobKey jobKey = new JobKey("xzwManual", "xzwQuartz");
		scheduler.resumeJob(jobKey);
	}
}
